﻿using System;
using System.Collections.Generic;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Enums;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageCommon
{
    public class PageCommon_DataList<TData> : ContainerBase
    {
        private PageCommon_QueryBox _query=null;
        private DataList_DataBlock<TData> _dataBlock = null;

        public PageCommon_DataList(ViewContext viewContext) : base(viewContext)
        {
            this.Css("box datalist");

            _query=new PageCommon_QueryBox(this.ViewContext);
            _dataBlock=new DataList_DataBlock<TData>(viewContext);
        }

        public DataList_DataBlock<TData> DataBlock => _dataBlock;

        public PageCommon_QueryBox FilterBlock => _query;

        public override void Dispose()
        {
            ViewContext.Writer.WriteLine("</div> <!-- .box-body .datalistcontent -->");

            ViewContext.Writer.WriteLine("</div> <!-- .box .datalist -->");
        }

        internal override ContainerBase RenderInternal()
        {
            ViewContext.Writer.WriteLine($"<div {Attributes}>");

            ViewContext.Writer.WriteLine("<div class=\"box-body no-padding datalistcontent\">");

            return this;
        }


        public static PageCommon_DataList<TData> operator +(PageCommon_DataList<TData> list, ColumnBase<TData> col2)
        {
            list.DataBlock.Table.Columns.AddColumn(col2);

            return list;
        }




        public class DataList_DataBlock<TData> : ContainerBase
        {
            private List<DataBlockButtonConfig> _leftButtons=new List<DataBlockButtonConfig>();
            private List<DataBlockButtonConfig> _rightButton=new List<DataBlockButtonConfig>();
            private DataListTable<TData> _data = null;

            public DataList_DataBlock(ViewContext viewContext) : base(viewContext)
            {
                this.Css("box box-table-area");

                _data=new DataListTable<TData>(this,viewContext);
                _data.Type(DataTableTypeEnum.WebUITable);
            }
            
            public DataList_DataBlock<TData> AddLeftSideButton(Action<DataBlockButtonConfig> btn)
            {
                var config=new DataBlockButtonConfig(ViewContext);

                config.Css("btn");

                btn(config);

                config.Css("btn-" + config._color);

                _leftButtons.Add(config);

                return this;
            }

            public DataList_DataBlock<TData> AddRightSideButton(Action<DataBlockButtonConfig> btn)
            {
                var config = new DataBlockButtonConfig(ViewContext);

                config.Css("btn");

                btn(config);

                config.Css("btn-" + config._color);

                _rightButton.Add(config);

                return this;
            }

            public PageCommon_Table2<TData> Table => _data;

            /// <summary>
            /// 返回table属性的Columns列集合,该属性用于适配+号方式的调用
            /// </summary>
            public TableColumns<TData> TableColumns => this.Table.Columns;

            public override void Dispose()
            {
                ViewContext.Writer.WriteLine("</div><!-- .box-body-content -->");
                
                ViewContext.Writer.WriteLine("</div><!--.box .box-table-area -->");

                if (_withOuterBorder)
                {
                    ViewContext.Writer.WriteLine($"</div> <!-- outer border -->");
                }
            }

            internal override ContainerBase RenderInternal()
            {
                if (_withOuterBorder)
                {
                    ViewContext.Writer.WriteLine($"<div class=\"{_outerBorderCss}\">");
                }

                ViewContext.Writer.WriteLine($"<div {Attributes}>");

                if (_leftButtons.HasData() || _rightButton.HasData())
                {
                    ViewContext.Writer.WriteLine("<div class=\"box-header with-border box-button-area\">");

                    if (_leftButtons.HasData())
                    {
                        ViewContext.Writer.WriteLine("<div class=\"box-title\">");

                        foreach (var btn in _leftButtons)
                        {
                            if (btn._hide)
                            {
                                continue;
                            }

                            btn.Command?.RenderToButton(btn);
                            var tag = btn._btnType == ButtonTypeEnum.link ? "a" : "button";
                            
                            ViewContext.Writer.Write($"<{tag} type=\"{btn._btnType}\" {btn.Attributes} >{btn._iconHtml}{btn.GetText}</{tag}>");
                        }

                        ViewContext.Writer.WriteLine("</div> <!-- .box-title -->");
                    }

                    if (_rightButton.HasData())
                    {

                        ViewContext.Writer.WriteLine("<div class=\"box-tools\">");

                        foreach (var btn in _rightButton)
                        {
                            if (btn._hide)
                            {
                                continue;
                            }


                            btn.Command?.RenderToButton(btn);
                            var tag = btn._btnType == ButtonTypeEnum.link ? "a" : "button";

                            ViewContext.Writer.Write($"<{tag} type=\"{btn._btnType}\" {btn.Attributes} >{btn._iconHtml}{btn.GetText}</{tag}>");
                        }
                        
                        ViewContext.Writer.WriteLine("</div><!-- .box-tools -->");
                    }

                    ViewContext.Writer.WriteLine("<div class=\"clearfix\"></div>");

                    ViewContext.Writer.WriteLine("</div> <!-- .box-header .box-button-area-->");
                }

                ViewContext.Writer.WriteLine("<div class=\"box-body box-body-content\">");
                

                return this;
            }

            public static DataList_DataBlock<TData> operator +(DataList_DataBlock<TData> block, ColumnBase<TData> col2)
            {
                block.Table.Columns.AddColumn(col2);

                return block;
            }

            public static DataList_DataBlock<TData> operator +(DataList_DataBlock<TData> block, PageCommon_Table2<TData> col2)
            {
                //block.Table.Columns.AddColumn(col2);

                return block;
            }


            public class DataBlockButtonConfig : ButtonConfigBase
            {
                internal ButtonTypeEnum _btnType = ButtonTypeEnum.button;

                public DataBlockButtonConfig ButtonType(ButtonTypeEnum type = ButtonTypeEnum.button)
                {
                    _btnType = type;

                    return this;
                }

                public DataBlockButtonConfig(ViewContext viewContext) : base(viewContext)
                {
                }
            }

            public class DataListTable<TData> : PageCommon_Table2<TData>
            {
                private DataList_DataBlock<TData> _block = null;

                public DataListTable(DataList_DataBlock<TData> block, ViewContext viewContext) : base(viewContext)
                {
                    IsRended = false;
                    IsDisposed = false;

                    _block = block;
                }

                internal bool IsRended { set; get; }

                internal bool IsDisposed { set; get; }

                internal override ContainerBase RenderInternal()
                {
                    var tbl= base.RenderInternal();
                    IsRended = true;
                    return tbl;
                }

                public override void Dispose()
                {
                    base.Dispose();

                }
            }
        }
    }
}
